package servlet;

import bean.Request;
import bean.Response;
import errors.HTTPMethodNotSupportException;
import utils.FileUtils;

import java.io.IOException;
import java.io.OutputStream;

public class DownloadServlet extends Servlet {
    @Override
    public void doBefore(Request request, Response response) throws HTTPMethodNotSupportException {
        response.setAttributes("Content-Type","application/octet-stream");
        response.setAttributes("Content-Disposition","attachment;fileName=" + "haha.gif");
        super.doBefore(request,response);
    }

    @Override
    public void doGet(Request request, Response response) throws HTTPMethodNotSupportException {
        OutputStream outputStream  = response.getOutputStream();
        try {
            outputStream.write(FileUtils.readFile("src/templates/haha.gif"));
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
